package at.ac.tuwien.swa.sparrow.common

import ac.at.tuwien.infosys.swa.audio.Fingerprint
import at.ac.tuwien.swa.sparrow._
import com.google.common.net.HostAndPort
import java.util.UUID
import scala.collection.mutable.ListBuffer
import scala.concurrent.duration.DurationDouble

case class AuthToken(id: Long, sid: String, timestamp: Long = System.currentTimeMillis) extends Serializable {
	def isExpired = (System.currentTimeMillis - timestamp).milliseconds > SESSION_DURATION
}

case class PeerInfo(id: Long, address: HostAndPort, lastOnline: Long, online: Boolean) extends Serializable {

	def this(p: Peer) = this(p.id, p.address, p.lastOnline, p.online)

	override def hashCode = id.hashCode

	override def equals(obj: Any) = obj match {
		case p: PeerInfo => this.id == p.id
		case _ => false
	}
}

case class Ticket(userId: Long, fingerPrint: Fingerprint, peers: ListBuffer[HostAndPort],
									hopCount: Int, id: UUID = UUID.randomUUID) extends Serializable {

	def isExpired = peers.size >= hopCount

	def isValid = hopCount >= 0

	override def toString = id.toString

	override def hashCode = id.hashCode

	override def equals(obj: Any) = obj match {
		case t: Ticket => this.id == t.id
		case _ => false
	}
}

case object TrackInfo {
	val unknownTrack = TrackInfo(null, "Unknown Track", "Unknown Artist", "Unknown Album")
}

case class TrackInfo(fingerPrint: Fingerprint, title: String = TrackInfo.unknownTrack.title,
										 artist: String = TrackInfo.unknownTrack.artist,
										 album: String = TrackInfo.unknownTrack.album) extends Serializable {

	override def toString = s"$artist - $title"
}

case class TrackResult(trackInfo: TrackInfo = TrackInfo.unknownTrack, state: IdentificationState) extends Serializable {
	override def toString = trackInfo.toString
}

abstract class IdentificationState

case object InProgress extends IdentificationState

case object Invalid extends IdentificationState

case object NotFound extends IdentificationState

case object Found extends IdentificationState
